package com.itheima.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.SmsFlashPromotionSessionMapper;
import com.itheima.pojo.SmsFlashPromotionProductRelation;
import com.itheima.pojo.SmsFlashPromotionSession;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author lenovo
 * @description 针对表【sms_flash_promotion_session(限时购场次表)】的数据库操作Service实现
 * @createDate 2022-10-11 14:26:17
 */
@DubboService
public class SmsFlashPromotionSessionApimpl implements SmsFlashPromotionSessionApi {

    @Autowired
    private SmsFlashPromotionSessionMapper flashPromotionSessionMapper;

    //添加场次
    @Override
    public void sessionCreate(SmsFlashPromotionSession flashPromotionSession) {
        flashPromotionSessionMapper.insert(flashPromotionSession);
    }

    //删除场次
    @Override
    public void sessionDelete(Long id) {
        flashPromotionSessionMapper.deleteById(id);

    }

    //获取全部场次
    @Override
    public List<SmsFlashPromotionSession> sessionList() {
        LambdaQueryWrapper<SmsFlashPromotionSession> lqw = Wrappers.lambdaQuery();
        return flashPromotionSessionMapper.selectList(lqw);
    }

    //获取全部可选场次及其数量
    @Override
    public List<SmsFlashPromotionSession> sessionSelectList(Long flashPromotionId) {
        LambdaQueryWrapper<SmsFlashPromotionSession> lqw = Wrappers.lambdaQuery();
        //status为1 是可用状态
        lqw.eq(SmsFlashPromotionSession::getStatus, 1)
                .orderByDesc(SmsFlashPromotionSession::getCreateTime);
        return flashPromotionSessionMapper.selectList(lqw);
    }


    //修改启用状态
    @Override
    public void updateStatus(Long id, Integer status) {
        //判断当前是否有这个id对应的实体
        SmsFlashPromotionSession session = flashPromotionSessionMapper.selectById(id);
        if (ObjectUtil.isNotNull(session)) {
            session.setStatus(status);
            flashPromotionSessionMapper.updateById(session);
        }
    }

    //修改场次
    @Override
    public void sessionUpdate(Integer id, SmsFlashPromotionSession flashPromotionSession) {
        SmsFlashPromotionSession session = flashPromotionSessionMapper.selectById(id);
        if (ObjectUtil.isNotNull(session)) {
            flashPromotionSessionMapper.updateById(flashPromotionSession);
        }
    }

    //获取场次详情
    @Override
    public SmsFlashPromotionSession findSessionById(Long id) {
        return flashPromotionSessionMapper.selectById(id);
    }
}




